using System.Collections;
using System.IO;
using System.Text;
using System;
using System.Windows.Forms;

namespace CSE360_SudokuProject
{
    public static class FileManager
    {
        private static string theFile = "./Data.ini"; 

        public static string userName;
        public static string passWord;
        private static bool newUser = false;
        private static int lineNumber = -1;
        public static float currentScore = 0;
        public static float currentTimeMinute = 0;
        public static float currentTimeSecond = 0; 
        public static int difficulty;
        //1 = easy, 2 = medium, 3 = hard, 4 = hell
        public static bool youWon = false;

        //time variables in seconds
        public static string[] highScoreEasyNames = new String[10];
        public static float[] highScoreEasyScores = new float[10];
        public static string[] highScoreEasyTimes = new string[10];   

        public static string[] highScoreMediumNames = new String[10];
        public static float[] highScoreMediumScores = new float[10];
        public static string[] highScoreMediumTimes = new string[10];

        public static string[] highScoreHardNames = new String[10];
        public static float[] highScoreHardScores = new float[10];
        public static string[] highScoreHardTimes = new string[10];

        public static string[] highScoreHellNames = new String[10];
        public static float[] highScoreHellScores = new float[10];
        public static string[] highScoreHellTimes = new string[10];

        public static void GetHighScore() //DeSerialize High Scores
        {
            if (File.Exists(theFile))//checks if file exits
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                string line;
                string[] lineArray;
                string[] delimiter = new string[1];
                delimiter[0] = " ";

                //reads in the scores and names a line at a time using the _delimiter to break up the strings
                #region HighScores
                //Easy scores, times and names
                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                highScoreEasyNames = lineArray;

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreEasyScores[i] = Convert.ToSingle(lineArray[i]);
                }

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreEasyTimes = lineArray;
                }

                //Medium scores, times and names
                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                highScoreMediumNames = lineArray;

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreMediumScores[i] = Convert.ToSingle(lineArray[i]);
                }

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreMediumTimes = lineArray;
                }

                //Hard scores, times and names
                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                highScoreHardNames = lineArray;

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreHardScores[i] = Convert.ToSingle(lineArray[i]);
                }

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreHardTimes = lineArray;
                }

                //Hell scores, times and names
                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                highScoreHellNames = lineArray;

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreHellScores[i] = Convert.ToSingle(lineArray[i]);
                }

                line = reader.ReadLine();
                lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < 10; i++)
                {
                    highScoreHellTimes = lineArray;
                }
                #endregion

                reader.Close();
            }
            else
            {
                //if the file doesn't exits, this files score arrays with generic values
                #region HighScores
                //easy
                for (int i = 0; i < 10; i++)
                {
                    highScoreEasyNames[i] = "aaa";
                    highScoreEasyScores[i] = 0;
                    highScoreEasyTimes[i] = "9999:9999";
                }

                //medium
                for (int i = 0; i < 10; i++)
                {
                    highScoreMediumNames[i] = "aaa";
                    highScoreMediumScores[i] = 0;
                    highScoreMediumTimes[i] = "9999:9999";
                }

                //hard
                for (int i = 0; i < 10; i++)
                {
                    highScoreHardNames[i] = "aaa";
                    highScoreHardScores[i] = 0;
                    highScoreHardTimes[i] = "9999:9999";
                }

                //hell
                for (int i = 0; i < 10; i++)
                {
                    highScoreHellNames[i] = "aaa";
                    highScoreHellScores[i] = 0;
                    highScoreHellTimes[i] = "9999:9999";
                }
                #endregion
            }
        }

        public static bool GetBoardData() //DeSerialize board data
        {
            if (File.Exists(theFile))//checks if file exits
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                string line;
                string[] lineArray;
                int convertTo2DAarray, lineCount;//used to convert linearray indext to a 2d index
                string[] delimiter = new string[1];
                delimiter[0] = " ";

                line = reader.ReadLine();
                while (!(line.CompareTo("EOF") == 0))//loops until EOF
                {
                    if (line.CompareTo("NextUser") == 0)//checks if at a user
                    {
                        line = reader.ReadLine();
                        if (line.CompareTo(userName) == 0)//checks if username entered exits at this point
                        {
                            line = reader.ReadLine();
                            if (line.CompareTo(passWord) == 0)//checks if password matches
                            {
                                line = reader.ReadLine();
                                if (line.CompareTo("is9x9") == 0) //checks if 9x9 is null
                                {
                                    ThePuzzleGenerator.is9x9 = true;
                                    ThePuzzleGenerator.is16x16 = false;
                                    line = reader.ReadLine();
                                    //loads time
                                    currentTimeMinute = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads time
                                    currentTimeSecond = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads score
                                    currentScore = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads difficulty
                                    difficulty = Convert.ToInt32(line);
                                    line = reader.ReadLine();
                                    //loads answer board
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardAnswer9x9 = new byte[9, 9];
                                    for (int i = 0; i < 9; i++)
                                    {
                                        convertTo2DAarray = lineCount * 9;
                                        for (int j = 0; j < 9; j++)
                                        {
                                            ThePuzzleGenerator.BoardAnswer9x9[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    line = reader.ReadLine();
                                    //loads Initial board 
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardInitial9x9 = new byte[9, 9];
                                    for (int i = 0; i < 9; i++)
                                    {
                                        convertTo2DAarray = lineCount * 9;
                                        for (int j = 0; j < 9; j++)
                                        {
                                            ThePuzzleGenerator.BoardInitial9x9[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    line = reader.ReadLine();
                                    //loads Current board 
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardCurrent9x9 = new byte[9, 9];
                                    for (int i = 0; i < 9; i++)
                                    {
                                        convertTo2DAarray = lineCount * 9;
                                        for (int j = 0; j < 9; j++)
                                        {
                                            ThePuzzleGenerator.BoardCurrent9x9[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    reader.Close();
                                    return true;
                                }
                                else if (line.CompareTo("is16x16") == 0)//checks if 16x16 is null
                                {
                                    ThePuzzleGenerator.is9x9 = false;
                                    ThePuzzleGenerator.is16x16 = true;
                                    line = reader.ReadLine();
                                    //loads time
                                    currentTimeMinute = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads time
                                    currentTimeSecond = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads score
                                    currentScore = Convert.ToSingle(line);
                                    line = reader.ReadLine();
                                    //loads difficulty
                                    difficulty = Convert.ToInt32(line);
                                    line = reader.ReadLine();
                                    //loads answer board
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardAnswer16x16 = new byte[16, 16];
                                    for (int i = 0; i < 16; i++)
                                    {
                                        convertTo2DAarray = lineCount * 16;
                                        for (int j = 0; j < 16; j++)
                                        {
                                            ThePuzzleGenerator.BoardAnswer16x16[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    line = reader.ReadLine();
                                    //loads Initial board
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardInitial16x16 = new byte[16, 16];
                                    for (int i = 0; i < 16; i++)
                                    {
                                        convertTo2DAarray = lineCount * 16;
                                        for (int j = 0; j < 16; j++)
                                        {
                                            ThePuzzleGenerator.BoardInitial16x16[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    line = reader.ReadLine();
                                    //loads Current board
                                    lineArray = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                                    convertTo2DAarray = 0;
                                    lineCount = 0;
                                    ThePuzzleGenerator.BoardCurrent16x16 = new byte[16, 16];
                                    for (int i = 0; i < 16; i++)
                                    {
                                        convertTo2DAarray = lineCount * 16;
                                        for (int j = 0; j < 16; j++)
                                        {
                                            ThePuzzleGenerator.BoardCurrent16x16[i, j] = Convert.ToByte(lineArray[j + convertTo2DAarray]);
                                        }
                                        lineCount++;
                                    }
                                    reader.Close();
                                    return true;
                                }
                                reader.Close();
                                return false;
                            }
                        }
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
            }
            return false;
        }

        public static bool CheckUser(String username, String password)
        {
            //checks if the user inputted correct info
            userName = username;
            passWord = password;

            if (ValidateUserSignIn())
                return true;
            else return false;
        }

        public static bool CreateNewUser(String username, String password)
        {
            //checks if the username is new and if so creates new user
            userName = username;
            passWord = password;

            if (ValidateNewUserName())
            {         
                newUser = true;
                SaveData();
                newUser = false;
                return true;
            }
            else return false;
        }

        private static bool ValidateNewUserName()
        {
            if (File.Exists(theFile))//checks if file exits
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                string line;

                line = reader.ReadLine();
                while (!(line.CompareTo("EOF") == 0))//loops until EOF
                {
                    if (line.CompareTo("NextUser") == 0)//checks if at a user
                    {
                        line = reader.ReadLine();
                        if (line.CompareTo(userName) == 0)//checks if username entered exits already if so returns false
                        {
                            reader.Close();
                            return false;
                        }
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
                return true;//if username is new returns true
     
            }
            return true;//if files doesn't exist the username is obviously new
        }

        private static bool ValidateUserSignIn()
        {
            if (File.Exists(theFile))//checks if file exits
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                string line;

                line = reader.ReadLine();
                while (!(line.CompareTo("EOF") == 0))//loops until EOF
                {
                    if (line.CompareTo("NextUser") == 0)//checks if at a user
                    {
                        line = reader.ReadLine();
                        if (line.CompareTo(userName) == 0)//checks if username entered exits at this point
                        {
                            line = reader.ReadLine();
                            if (line.CompareTo(passWord) == 0)//checks if password matches if so returns true
                            {
                                reader.Close();
                                return true;
                            }
                        }
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
                return false;//if info is wrong returns false

            }
            return false;//if files doesn't exist the info is obviously wrong
        }

        //need to add in spot for answerboard
        public static void SaveData() // Serialize High Scores
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (!File.Exists(theFile)) //if file doesn't exist creates generic highscore values
                GetHighScore();
            
                //code that saves the new highscore values
                #region HighScores
                //Easy scores and nammes
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreEasyNames[i] + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreEasyScores[i].ToString() + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreEasyTimes[i].ToString() + " ");
                }
                sb.AppendLine();

                //Medium scores and names
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreMediumNames[i] + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreMediumScores[i].ToString() + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreMediumTimes[i].ToString() + " ");
                }
                sb.AppendLine();

                //Hard scores and names
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHardNames[i] + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHardScores[i].ToString() + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHardTimes[i].ToString() + " ");
                }
                sb.AppendLine();

                //Hell scores and names
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHellNames[i] + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHellScores[i].ToString() + " ");
                }
                sb.AppendLine();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append(highScoreHellTimes[i].ToString() + " ");
                }
                sb.AppendLine();
                #endregion

                //counts to the line in the file that board data is on
                lineNumber = SaveBoard();

                //function to copy the rest of the file to the string
                sb.Append(ContinueWrite(lineNumber));

                //code to append a new user to the end of file
                #region NewUser
                if (newUser == true)
                {
                    sb.AppendLine("NextUser");
                    sb.AppendLine(userName);
                    sb.AppendLine(passWord);

                    if (ThePuzzleGenerator.BoardAnswer9x9 != null)//if 9x9 exits, append data
                    {
                        sb.AppendLine("is9x9");
                        sb.AppendLine(currentTimeMinute.ToString());
                        sb.AppendLine(currentTimeSecond.ToString());
                        sb.AppendLine(currentScore.ToString());
                        sb.AppendLine(difficulty.ToString());
                        for (int i = 0; i < 9; i++)
                            for (int j = 0; j < 9; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardAnswer9x9[i, j] + " ");
                            }
                    }
                    else if (ThePuzzleGenerator.BoardAnswer16x16 != null)//if 16x16 exist append data
                    {
                        sb.AppendLine("is16x16");
                        sb.AppendLine(currentTimeMinute.ToString());
                        sb.AppendLine(currentTimeSecond.ToString());
                        sb.AppendLine(currentScore.ToString());
                        sb.AppendLine(difficulty.ToString());
                        for (int i = 0; i < 16; i++)
                            for (int j = 0; j < 16; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardAnswer16x16[i, j] + " ");
                            }
                    }
                    else
                    {
                        sb.AppendLine();// adds line for is9x9 or is16x16 line
                        sb.AppendLine();// adds line for current minute line
                        sb.AppendLine();// adds line for current second line
                        sb.AppendLine();// adds line for current score line
                        sb.AppendLine();// adds line for difficulty line
                    }

                    sb.AppendLine();//adds line for answer board data if not there or moves to next line if it exits

                    if (ThePuzzleGenerator.BoardInitial9x9 != null)//if 9x9 exits, append data
                        for (int i = 0; i < 9; i++)
                            for (int j = 0; j < 9; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardInitial9x9[i, j] + " ");
                            }
                    else if (ThePuzzleGenerator.BoardInitial16x16 != null)//if 16x16 exist append data
                        for (int i = 0; i < 16; i++)
                            for (int j = 0; j < 16; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardInitial16x16[i, j] + " ");
                            }
                    sb.AppendLine();//adds line for Intial board data if not there or moves to next line if it exits

                    if (ThePuzzleGenerator.BoardCurrent9x9 != null)//if 9x9 exits, append data
                        for (int i = 0; i < 9; i++)
                            for (int j = 0; j < 9; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardCurrent9x9[i, j] + " ");
                            }
                    else if (ThePuzzleGenerator.BoardCurrent16x16 != null)//if 16x16 exist append data
                        for (int i = 0; i < 16; i++)
                            for (int j = 0; j < 16; j++)
                            {
                                sb.Append(ThePuzzleGenerator.BoardCurrent16x16[i, j] + " ");
                            }
                    sb.AppendLine();//adds line for Current board data if not there or moves to next line if it exits
                    sb.Append("EOF"); //attach EOF to end
                }
                #endregion

                System.IO.StreamWriter writer = new System.IO.StreamWriter(theFile);
                writer.Write(sb.ToString());
                writer.Close();
        }

        //doesn't add space for null boards
        private static string ContinueWrite(int _lineNumber)
        {
            string line = "";
            int count = 0;
            System.Text.StringBuilder userSection = new System.Text.StringBuilder();
            if (File.Exists(theFile)) //checks to make sure file exits if not just appends EOF
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                line = reader.ReadLine();
                count++;
                while (!(line.CompareTo("EOF") == 0) && !(line.CompareTo("NextUser") == 0)) // goes to the first user
                {
                    line = reader.ReadLine();
                    count++;
                }
                while (!(line.CompareTo("EOF") == 0)) //loops through until EOF appending info to string
                {
                    if (count == _lineNumber)//if count is equal to line number then a board exists and is at this line
                    {
                        if (ThePuzzleGenerator.BoardAnswer9x9 != null)//if 9x9 exits, append data
                        {
                            userSection.AppendLine("is9x9");
                            userSection.AppendLine(currentTimeMinute.ToString());
                            userSection.AppendLine(currentTimeSecond.ToString());
                            userSection.AppendLine(currentScore.ToString());
                            userSection.AppendLine(difficulty.ToString());
                            for (int i = 0; i < 9; i++)
                                for (int j = 0; j < 9; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardAnswer9x9[i, j] + " ");
                                }
                        }
                        else if (ThePuzzleGenerator.BoardAnswer16x16 != null)//if 16x16 exist append data
                        {
                            userSection.AppendLine("is16x16");
                            userSection.AppendLine(currentTimeMinute.ToString());
                            userSection.AppendLine(currentTimeSecond.ToString());
                            userSection.AppendLine(currentScore.ToString());
                            userSection.AppendLine(difficulty.ToString());
                            for (int i = 0; i < 16; i++)
                                for (int j = 0; j < 16; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardAnswer16x16[i, j] + " ");
                                }
                        }
                        else
                        {
                            userSection.AppendLine();//adds line for is9x9 or is16x16
                            userSection.AppendLine();//adds line for current minute
                            userSection.AppendLine();//adds line for current second
                            userSection.AppendLine();//adds line for current score
                            userSection.AppendLine();//adds line for difficulty
                        }
                        userSection.AppendLine();//adds line for answer board data

                        if (ThePuzzleGenerator.BoardInitial9x9 != null)//if 9x9 exits, append data
                            for (int i = 0; i < 9; i++)
                                for (int j = 0; j < 9; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardInitial9x9[i, j] + " ");
                                }
                        else if (ThePuzzleGenerator.BoardInitial16x16 != null)//if 16x16 exist append data
                            for (int i = 0; i < 16; i++)
                                for (int j = 0; j < 16; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardInitial16x16[i, j] + " ");
                                }
                        userSection.AppendLine();//adds line for Intial board data

                        if (ThePuzzleGenerator.BoardCurrent9x9 != null)//if 9x9 exits, append data
                            for (int i = 0; i < 9; i++)
                                for (int j = 0; j < 9; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardCurrent9x9[i, j] + " ");
                                }
                        else if (ThePuzzleGenerator.BoardCurrent16x16 != null)//if 16x16 exist append data
                            for (int i = 0; i < 16; i++)
                                for (int j = 0; j < 16; j++)
                                {
                                    userSection.Append(ThePuzzleGenerator.BoardCurrent16x16[i, j] + " ");
                                }
                        userSection.AppendLine();//adds line for Current board data

                        line = reader.ReadLine();//moves it from is9x9 or is16x16 line
                        line = reader.ReadLine();//moves it from current minute line
                        line = reader.ReadLine();//moves it from current second line
                        line = reader.ReadLine();//moves it from current score line
                        line = reader.ReadLine();//moves it from difficulty line
                        count++;
                        line = reader.ReadLine();//moves reader down to second board data
                        line = reader.ReadLine();//moves reader down to third board data so that the next readline pushes past board data
                        count++;//moves the counter away from the line number so doesn't double update board data
                    }
                    else
                        userSection.AppendLine(line);

                    line = reader.ReadLine();
                    count++;
                }
                if (newUser != true) // checks if new user is being added, if so doesn't append the EOF yet
                    userSection.Append(line);
                reader.Close();
            }
            else
                if (newUser != true) // checks if new user is being added, if so doesn't append the EOF yet
                    userSection.Append("EOF");

                return userSection.ToString();
        }

        private static int SaveBoard()
        {
            int lineNumber = 0;
            if (File.Exists(theFile))//checks if file exits
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(theFile);
                string line;

                line = reader.ReadLine();
                lineNumber++;
                while (!(line.CompareTo("EOF") == 0))//loops until EOF
                {
                    if (line.CompareTo("NextUser") == 0)//checks if at a user
                    {
                        line = reader.ReadLine();
                        lineNumber++;
                        if (line.CompareTo(userName) == 0)//checks if username entered exits at this point
                        {
                            line = reader.ReadLine();
                            lineNumber++;
                            if (line.CompareTo(passWord) == 0)//checks if password matches
                            {
                                lineNumber++; //updates the lineNumber to the line with the board data to be updated
                                reader.Close();
                                return lineNumber;
                            }
                        }
                    }
                    line = reader.ReadLine();
                    lineNumber++; 
                } 
                reader.Close();
                lineNumber = -1; //reached EOF without finding username and password
                return lineNumber; 
            }
            lineNumber = -1; //file doesn't exist
            return lineNumber; 
        }
    }
}